//
//  KKLiveChatViewController.m
//  KKLiveDemo
//
//  Created by leo on 2017/7/29.
//  Copyright © 2017年 leo. All rights reserved.
//

#import "KKLiveChatViewController.h"

#import "KKLiveMessageBaseTableViewCell.h"
#import "KKMessageItem+UITableViewCell.h"
#import "KKLiveHelper.h"

//#define KKLIVE_SCREEN_WIDTH          [UIScreen mainScreen].bounds.size.width   //整个屏幕的宽度
//#define KKLIVE_SCREEN_HEIGHT         [UIScreen mainScreen].bounds.size.height  //整个屏幕的高度

@interface KKLiveChatViewController ()

@property (nonatomic, assign) CGFloat keyboardPointY;

@end

@implementation KKLiveChatViewController

#pragma mark TestData 

- (void)test {
    
    for (NSInteger index = 0; index < 0; index++) {
        KKMessageItem *item = [KKMessageItem msgWithText: @"dsfsfsdf"];
        [self.arr_messageItems addObject: item];
    }
}

- (void)loadView {
    [super loadView];
    _toolBar = [[KKLiveChatToolBarView alloc] initWithFrame: CGRectMake(0, KKLIVE_SCREEN_HEIGHT - 50, KKLIVE_SCREEN_WIDTH, 50)];
    _toolBar.delegate = self;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
    [self.view addSubview: _toolBar];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self private_registerNotification];
    _arr_messageItems = [NSMutableArray array];
    [self test];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver: self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Notification methods
- (void)private_registerNotification {
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(notication_keyboardChange:) name: UIKeyboardWillChangeFrameNotification object: nil];
}

- (void)notication_keyboardChange:(NSNotification*)notification {
    
    NSDictionary *userInfo = notification.userInfo;
    CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat pointY = CGRectGetMinY(endFrame);
    self.keyboardPointY = pointY;
    
    [UIView animateWithDuration: 0.25 animations:^{
        _toolBar.center = CGPointMake(KKLIVE_SCREEN_WIDTH/2, pointY - CGRectGetHeight(_toolBar.bounds)/2);
    } completion:^(BOOL finished) {
        [self.view layoutIfNeeded];
    }];
    
    if (self.arr_messageItems.count) {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:MAX(0, self.arr_messageItems.count - 1)] atScrollPosition:UITableViewScrollPositionBottom animated: false];
    }
}

#pragma mark - KKLiveChatToolBarViewDelegate methods

- (void)liveChatToolBarDidSendMsg:(KKMessageItem *)msg {
    //  发送一条消息出去
    [_arr_messageItems addObject: msg];
    if (_arr_messageItems.count == 1) {
        [self.tableView reloadData];
    }
    else {
        NSIndexPath *insertIndexPath = [NSIndexPath indexPathForRow: _arr_messageItems.count - 1 inSection: 0];
        [self.tableView insertRowsAtIndexPaths: @[insertIndexPath] withRowAnimation: UITableViewRowAnimationNone];
        [self.tableView scrollToRowAtIndexPath: insertIndexPath atScrollPosition: UITableViewScrollPositionNone animated: YES];
    }
}

- (void)liveChatToolBarDidUpdateBarHeight:(CGFloat)height {
    
    if (self.keyboardPointY != KKLIVE_SCREEN_HEIGHT && self.keyboardPointY != 0) {
        _toolBar.center = CGPointMake(KKLIVE_SCREEN_WIDTH/2, self.keyboardPointY - CGRectGetHeight(_toolBar.bounds)/2);
    }
    else if (self.keyboardPointY == KKLIVE_SCREEN_HEIGHT) {
        _toolBar.center = CGPointMake(KKLIVE_SCREEN_WIDTH/2, KKLIVE_SCREEN_HEIGHT - CGRectGetHeight(_toolBar.bounds)/2);
    }
}

#pragma mark - Private methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_arr_messageItems count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    KKMessageItem *messageItem = [self.arr_messageItems objectAtIndex: indexPath.row];
    return [messageItem heightInWidth: KKLIVE_SCREEN_WIDTH style: KKLiveMessageBaseTableViewCellStyle_Group];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    KKMessageItem *messageItem = [self.arr_messageItems objectAtIndex: indexPath.row];
    
    KKLiveMessageBaseTableViewCell *cell = [messageItem tableView: tableView style:KKLiveMessageBaseTableViewCellStyle_Group];
    [cell configWithMsg: messageItem];
    return cell;
}

@end
